package dwarffortress.loaders;

import dwarffortress.data.DFLanguageWord;

import java.io.Reader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Loads raw file language_words.txt
 */
public class LanguageLoader extends RawLoader {

    private Map<String, DFLanguageWord> dictionary;
    private DFLanguageWord currentWord;
    private String[] currentWords;
    private DFLanguageWord.WordMode currentMode;


    public Map<String, DFLanguageWord> loadLanguage(Reader reader) throws IOException{
        dictionary = new TreeMap<String, DFLanguageWord>();
        readRaw(reader);
        startWord("");
        dictionary.remove("");
        return dictionary;
    }

    protected void handleElement(String element) throws IOException {
        if (element.startsWith("OBJECT:")) {
            //Just ignore
            return;
        } else if (element.startsWith("WORD:")) {
            startWord(element.substring(5));
        } else if (element.startsWith("ADJ:")) {
            startNewMode(DFLanguageWord.WordMode.ADJ, element.substring(4).split(":"));
        } else if (element.startsWith("PREFIX:")) {
            startNewMode(DFLanguageWord.WordMode.PREFIX, element.substring(7).split(":"));
        } else if (element.startsWith("NOUN:")) {
            startNewMode(DFLanguageWord.WordMode.NOUN, element.substring(5).split(":"));
        } else if (element.startsWith("VERB:")) {
            startNewMode(DFLanguageWord.WordMode.VERB, element.substring(5).split(":"));
        } else if (element.equals("FRONT_COMPOUND_NOUN_PLUR")) {
            currentWord.addLocation(DFLanguageWord.WordMode.SPECIAL_NOUN_PLURAL, DFLanguageWord.WordLocation.FRONT_COMPOUND);
        } else if (element.equals("FRONT_COMPOUND_NOUN_SING")) {
            currentWord.addLocation(DFLanguageWord.WordMode.NOUN, DFLanguageWord.WordLocation.FRONT_COMPOUND);
        } else if (element.equals("REAR_COMPOUND_NOUN_PLUR")) {
            currentWord.addLocation(DFLanguageWord.WordMode.SPECIAL_NOUN_PLURAL, DFLanguageWord.WordLocation.REAR_COMPOUND);
        } else if (element.equals("REAR_COMPOUND_NOUN_SING")) {
            currentWord.addLocation(DFLanguageWord.WordMode.NOUN, DFLanguageWord.WordLocation.REAR_COMPOUND);
        } else if (element.equals("THE_NOUN_PLUR")) {
            currentWord.addLocation(DFLanguageWord.WordMode.SPECIAL_NOUN_PLURAL, DFLanguageWord.WordLocation.THE_WORD);
        } else if (element.equals("THE_NOUN_SING")) {
            currentWord.addLocation(DFLanguageWord.WordMode.NOUN, DFLanguageWord.WordLocation.THE_WORD);
        } else if (element.equals("OF_NOUN_PLUR")) {
            currentWord.addLocation(DFLanguageWord.WordMode.SPECIAL_NOUN_PLURAL, DFLanguageWord.WordLocation.THE_WORD);
        } else if (element.equals("OF_NOUN_SING")) {
            currentWord.addLocation(DFLanguageWord.WordMode.NOUN, DFLanguageWord.WordLocation.THE_WORD);
        } else if (element.equals("THE_COMPOUND_NOUN_PLUR")) {
            currentWord.addLocation(DFLanguageWord.WordMode.SPECIAL_NOUN_PLURAL, DFLanguageWord.WordLocation.OF_WORD);
        } else if (element.equals("THE_COMPOUND_NOUN_SING")) {
            currentWord.addLocation(DFLanguageWord.WordMode.NOUN, DFLanguageWord.WordLocation.OF_WORD);
        } else if (element.equals("THE_COMPOUND_PREFIX")) {
            currentWord.addLocation(DFLanguageWord.WordMode.PREFIX, DFLanguageWord.WordLocation.OF_WORD);
        } else if (element.equals("THE_COMPOUND_ADJ")) {
            currentWord.addLocation(DFLanguageWord.WordMode.ADJ, DFLanguageWord.WordLocation.OF_WORD);
        } else if (element.equals("FRONT_COMPOUND_ADJ")) {
            currentWord.addLocation(DFLanguageWord.WordMode.ADJ, DFLanguageWord.WordLocation.FRONT_COMPOUND);
        } else if (element.equals("FRONT_COMPOUND_PREFIX")) {
            currentWord.addLocation(DFLanguageWord.WordMode.PREFIX, DFLanguageWord.WordLocation.FRONT_COMPOUND);
        } else if (element.equals("REAR_COMPOUND_ADJ")) {
            currentWord.addLocation(DFLanguageWord.WordMode.ADJ, DFLanguageWord.WordLocation.REAR_COMPOUND);
        } else if (element.equals("STANDARD_VERB")) {
            currentWord.markStandardVerb();
        } else if (element.startsWith("ADJ_DIST:")) {
            currentWord.setAdjectiveDistance(Integer.parseInt(element.substring("ADJ_DIST:".length())));
        } else {
            throw new IOException("Unknow raw element " + element);
        }
    }

    private void startNewMode(DFLanguageWord.WordMode mode, String[] strings) {
        currentMode = mode;
        currentWords = strings;
        currentWord.addWords(mode, strings);
    }

    private void startWord(String s) {
        startNewMode(DFLanguageWord.WordMode.SPECIAL_OFF, null);
        currentWord = new DFLanguageWord();
        dictionary.put(s, currentWord);
    }
}
